﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model.Cust;

namespace Backend.JYD.Business
{
    public class CustManager
    {
        #region static
        
        private static readonly IDAL.Cust.ICustBase idal = Factory.DALFactory.CreateEFDAL<IDAL.Cust.ICustBase>("DAL.CustBaseDAL");
        private static readonly IDAL.Cust.ICustPhone idalPhone = Factory.DALFactory.CreateEFDAL<IDAL.Cust.ICustPhone>("DAL.CustPhoneDAL");

        static object _lock = new object();
        static Dictionary<string, int> numKey = new Dictionary<string, int>();

        #endregion

        #region enum

        public enum CustType { 正式客户号 = 1, 临时客户号 = 2 }
        public enum CustChanelType { 线上营销 = 1, 线上推荐 = 2, 线下营销 = 3, 线下推荐 = 4 }

        #endregion

        #region 客户号

        public CustType GetCustTypeByCustNo(string custNo)
        {
            if (IsFormalCust(custNo)) return CustType.正式客户号;
            return CustType.临时客户号;
        }

        private string PerfixCustType(CustType type)
        {
            switch (type)
            {
                case CustType.正式客户号:
                    return "C";
                case CustType.临时客户号:
                default:
                    return "Q";
            }
        }
        private string PerfixCustChanelType(CustChanelType type)
        {
            switch (type)
            {
                case CustChanelType.线下营销:
                    return "4";
                case CustChanelType.线下推荐:
                    return "3";
                case CustChanelType.线上推荐:
                    return "2";
                case CustChanelType.线上营销:
                default:
                    return "1";
            }
        }

        /// <summary>
        /// 正式客户号
        /// </summary>
        public static bool IsFormalCust(string custNo)
        {
            return custNo.StartsWith("C");
        }
        private string CreateCustNo(CustChanelType chanel)
        {
            return CreateCustNo(CustType.正式客户号, chanel);
        }
        private string CreateCustNo(CustType custType, CustChanelType chanel)
        {
            string custNo = PerfixCustType(custType) + PerfixCustChanelType(chanel);//PerfixCustType(type)

            int year = DateTime.Today.Year;
            int month = DateTime.Today.Month;
            int day = DateTime.Today.Day;
            int hour = DateTime.Now.Hour;
            int min = DateTime.Now.Minute;
            int sec = DateTime.Now.Second;
            int mill = DateTime.Now.Millisecond;

            string key = string.Empty;
            //year = (year % 100 - 13) % 36;
            year = year % 100;
            //key += (year < 10) ? year.ToString() : Convert.ToChar(year + 55).ToString();
            key += (year < 10) ? "0" + year.ToString() : year.ToString();
            key += (month < 10) ? month.ToString() : Convert.ToChar(month + 55).ToString();
            key += (day < 10) ? day.ToString() : Convert.ToChar(day + 55).ToString();
            key += (hour < 10) ? hour.ToString() : Convert.ToChar(hour + 55).ToString();
            key += (min < 10) ? "0" + min.ToString() : min.ToString();
            key += (sec < 10) ? "0" + sec.ToString() : sec.ToString();
            key += mill.ToString();

            int keyNum = 0;
            lock (_lock)
            {
                if (numKey.ContainsKey(key))
                {
                    numKey[key] = ++numKey[key];
                }
                else
                {
                    numKey.Add(key, 0);
                }
                keyNum = numKey[key];
            }

            custNo += key + keyNum.ToString();
            return custNo;
        }

        #endregion
        /// <summary>
        /// ifExeistingAllowEidtPaper：权限预设，是否允许修改证件。
        /// </summary>
        /// <returns></returns>
        public string AddCustBase(CustBase info, bool ifExeistingAllowEidtPaper)
        {
            if (string.IsNullOrWhiteSpace(info.CustNo))
            {
                bool success = false;
                bool isDefault = false;
                if (!string.IsNullOrWhiteSpace(info.PaperType) && !string.IsNullOrWhiteSpace(info.PaperNo) && !string.IsNullOrWhiteSpace(info.CustName))
                {
                    var temp = idal.GetCustBaseByPaper(info.PaperType, info.PaperNo, info.CustName);
                    if (temp != null)
                    {
                        info.CustNo = temp.CustNo;
                        info.Type = temp.Type;
                        isDefault = temp.Phone != info.Phone;
                        if (!ifExeistingAllowEidtPaper)
                        {
                            success = idal.UpdateCustBaseInfo(info);
                        }
                        else
                        {
                            success = idal.UpdateCustBase(info);
                        }
                    }
                    else
                    {
                        info.Type = (int)CustType.正式客户号;
                        info.CustNo = this.CreateCustNo((CustType)info.Type, (CustChanelType)info.ChanelType);
                        info.CustNo = idal.AddCustBase(info);
                        success = string.IsNullOrWhiteSpace(info.CustNo);
                    }
                }
                else
                {
                    info.PaperNo = string.Empty;
                    info.PaperType = string.Empty;
                    info.Type = (int)CustType.临时客户号;
                    isDefault = true;
                    info.CustNo = idal.AddCustBase(info);
                }

                success = idalPhone.CheckCustPhone(info.CustNo, info.Phone);
                if (!success)
                {
                    idalPhone.AddCustPhone(new CustPhone() { CustNo = info.CustNo, Phone = info.Phone, IsDefault = isDefault });
                }
            }
            return info.CustNo;
        }

    }
}
